package com.duola.security.distributed.order.dao;

import com.duola.security.distributed.order.model.PermissionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-12-29 2:14
 */
public class PermissionDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<String> findPermissionsByUserId(String id) {
        String sql = "SELECT *  FROM t_permission WHERE id in (\n" +
                "\tSELECT permission_id from t_role_permission WHERE role_id in (\n" +
                "\t\tSELECT role_id FROM t_user_role WHERE user_id =?\n" +
                "\t)\n" +
                "\t\n" +
                ")";
        List<PermissionDto> list = jdbcTemplate.query(sql, new Object[]{id}, new BeanPropertyRowMapper<>(PermissionDto.class));
        System.out.println(list);

        ArrayList<String> permissions = new ArrayList<>();
        list.iterator().forEachRemaining(c ->permissions.add(c.getCode()));
        return permissions;
    }
}
